﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmPhong : PRE.catalog.FrmBase
    {
        DAL.PhongDAL _dal = new DAL.PhongDAL();
        BLL.PhongBLL _bll = new BLL.PhongBLL();
        public FrmPhong()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);

            //string _ma = _bll.CreateID();
            //txtSophong.Text = _bll.CreateID();
            txtSophong.ResetText();
            txtTenphong.ResetText();
            cboMakhoa.ResetText();
            cboMakhoa.Focus();



            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string _sophong = txtSophong.Text.Trim();
            string _makhoa = (cboMakhoa.EditValue + "").Trim();
            var o = new DAL.Entities.phong()

            {
                sophong =Convert.ToInt32(_sophong),
                makhoa = _makhoa
            };

            DialogResult result;
            var ok = _dal.deleteobj(o);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            txtTenphong.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var name = Proper(txtTenphong.Text);
            var namekhoa = (cboMakhoa.EditValue + "").Trim();

            if (IsAdd)
            {
                var o = new DAL.Entities.phong()
                {
                    sophong =Convert.ToInt32(txtSophong.Text.Trim()),
                    tenphong= name,
                    makhoa = namekhoa.Trim()
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                string _sophong = txtSophong.Text.Trim();
                var o = new DAL.Entities.phong()
                {
                    sophong = Convert.ToInt32(_sophong),
                    tenphong = name,
                    
                    makhoa = (cboMakhoa.EditValue + "").Trim()

                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtSophong.Text = null;
            txtTenphong.Text = null;
            
            cboMakhoa.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtTenphong.DataBindings.Clear();
            txtSophong.DataBindings.Clear();
            
            cboMakhoa.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtSophong.DataBindings.Add("EditValue", _dtb, ".SOPHONG");
            txtTenphong.DataBindings.Add("EditValue", _dtb, ".TENPHONG");
            
            cboMakhoa.DataBindings.Add("EditValue", _dtb, ".MAKHOA");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtTenphong.Properties.ReadOnly = isReadOnly;
            //dteNamsinh.Properties.ReadOnly = isReadOnly;
            //radioGioitinh.Properties.ReadOnly = isReadOnly;
            //txtDiachi.Properties.ReadOnly = isReadOnly;
            //txtDienthoai.Properties.ReadOnly = isReadOnly;
            //txtSophong.Properties.ReadOnly = isReadOnly;
            cboMakhoa.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion

        private void FrmPhong_Load(object sender, EventArgs e)
        {
            txtSophong.Properties.ReadOnly = true;
            DataTable _dtb2 = _dal.Select_khoa();
            cboMakhoa.Properties.DataSource = _dtb2;

            PerformRefresh();

        }

        private void cboMakhoa_EditValueChanged(object sender, EventArgs e)
        {
            string a = (cboMakhoa.EditValue + "").Trim();
            txtSophong.Text = _bll.CreateID(a);
        }

  
    }
}
